<?php
//
// +------------------------------------------------------------------------+
// | PHP Version 5.4                                                        |
// +------------------------------------------------------------------------+
// | Copyright (c) All rights reserved.                                     |
// +------------------------------------------------------------------------+
// | 世纪车来车往(北京）网络科技有限公司 版权所有！                               |
// | TEL:010-63732365  HOTLINE:400-6688-365  EMAIL:clcw@clcw.com.cn         |
// | $Revision: 1.0 $                                                       |
// +------------------------------------------------------------------------+
// | Author: xiangminghu@clcw.com.cn                                        |
// +------------------------------------------------------------------------+
//

namespace AuctionBack\Logic;

use AuctionCore\Common\PhpRedis;

class CommentLogic extends BaseLogic
{
    protected $autoCheckFields = false;

    public function getArticleComment($commentID)
    {
        return D('Comment', 'Model')->where(['comment_id' => $commentID])->find();
    }

    public function getExpertArticleComment($commentID)
    {
        return D('ExpertComment', 'Model')->where(['comment_id' => $commentID])->find();
    }
    
    public function getArticleCommentList($table, $params, $page, $pageSize)
    {
        $where = [];
        if ($params['comment_id']) {
            $where['c.comment_id'] = $params['comment_id'];
        }
        if ($params['content']) {
            $where['c.content'] = ['like', "%{$params['content']}%"];
        }
        if ($params['user_id']) {
            $where['c.user_id'] = $params['user_id'];
        }
        if ($params['mobile']) {
            $where['u.mobile'] = $params['mobile'];
        }
        if ($params['article_id']) {
            $where['c.article_id'] = $params['article_id'];
        }
        if ($params['status']) {
            $where['c.status'] = $params['status'];
        }
        if ($params['report']) {
            if ($params['report'] == 1) {
                $where['report_num'] = ['gt', 0];
            } else {
                $where['report_num'] = ['eq', 0];
            }
        }
        if ($params['start_time']) {
            $where['create_time'] = ['gt', strtotime($params['start_time'])];
        }
        if ($params['end_time']) {
            $where['create_time'] = ['lt', strtotime($params['end_time'])];
        }

        $total = M($table . ' c', 'clcw_', 'DB_CLCW')
            ->join('LEFT JOIN clcw_user u ON (c.user_id = u.user_id)')
            ->where($where)
            ->count();

        $list = M($table . ' c', 'clcw_', 'DB_CLCW')
            ->field('c.comment_id, c.content, u.user_id, u.nick_name, u.mobile, c.article_id, c.status, c.report_num, c.create_time')
            ->join('LEFT JOIN clcw_user u ON (c.user_id = u.user_id)')
            ->where($where)
            ->page($page, $pageSize)
            ->order('c.comment_id DESC')
            ->select();

        return ['total' => $total, 'list' => $list];
    }

    /**
     * 修改资讯评论
     * @param $data
     * @return boold
     */
    public function saveArticleComment($data)
    {
        $id = $data['comment_id'];
        if (!$id) {
            return false;
        }
        unset($data['id']);

        if ($data['status'] == 3) {
            $comment = $this->getArticleComment($id);
            $this->desScore($comment['user_id']);
            $this->desArticleComment($comment['article_id']);
        }

        $res = D('Comment', 'Model')->where(['comment_id' => $id])->save($data);
        return $res;
    }


    /**
     * 减少资讯评论数
     * @param $articleID
     * @return boold
     */
    public function desArticleComment($articleID)
    {
        return D('Article', 'Model')->where(['article_id' => $articleID])->setDec('comment', 1);
    }


    /**
     * 修改日报评论
     * @param $data
     * @return boold
     */
    public function saveExpertArticleComment($data)
    {
        $id = $data['comment_id'];
        if (!$id) {
            return false;
        }
        unset($data['id']);

        if ($data['status'] == 3) {
            $comment = $this->getExpertArticleComment($id);
            $this->desScore($comment['user_id']);
            $this->desExpertArticleComment($comment['article_id']);
        }

        $res = D('ExpertComment', 'Model')->where(['comment_id' => $id])->save($data);
        return $res;
    }

    /**
     * 减少日报评论数
     * @param $articleID
     * @return boold
     */
    public function desExpertArticleComment($articleID)
    {
        return D('ArticleExpert', 'Model')->where(['article_id' => $articleID])->setDec('comment', 1);
    }

    /**
     * 扣除用户积分、成长值
     * @param $userID
     * @return bool
     */
    private function desScore($userID)
    {
        $redis = new PhpRedis(0);
        $keyName = getRedisKeyByCode('REDIS_KEY', '100000');
        //禁用评论扣除积分
        $res = $redis->lpush($keyName, json_encode(
                [
                    'bizContent' =>
                        [
                            'scoreType' => 58,
                            'score'     => 0,
                            'remark'    => '',
                            'userId'    => (int)$userID
                        ],
                    'bizType'    => 5
                ]
            )
        );
        //禁用评论扣除成长值
        $res2 = $redis->lpush($keyName, json_encode(
                [
                    'bizContent' =>
                        [
                            'expType' => 58,
                            'exp'     => 0,
                            'remark'  => '',
                            'userId'  => (int)$userID
                        ],
                    'bizType'    => 6
                ]
            )
        );

        return $res && $res2;
    }
}
